//
// Created by 11510 on 2024/6/16.
//
#include <iostream>
#include <regex>
#include <string>

int main() {
    std::string text = "The quick brown fox jumps over the lazy dog.";
    std::regex word_regex(R"(\b\w+\b)");
    std::smatch match_results;  /* smatch 好像是指针数组*/

    // 使用 regex_search 在字符串中搜索第一个匹配项
    if (std::regex_search(text, match_results, word_regex)) {
        std::cout << "First match: " << match_results[0] << std::endl;
    }

    // 使用 regex_search 在字符串中搜索所有匹配项
    auto words_begin = std::sregex_iterator(text.begin(), text.end(), word_regex);
    auto words_end = std::sregex_iterator();

    std::cout << "All matches:" << std::endl;
    for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
        std::smatch match = *i;
        std::string match_str = match.str();
        std::cout << match_str << std::endl;
    }

    // 使用 regex_replace 替换字符串中的匹配项
    std::string replaced_text = std::regex_replace(text, word_regex, "[$&]");
    std::cout << "Replaced text: " << replaced_text << std::endl;

    return 0;
}
